import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Header from '../components/header.vue'
const Release = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Release.vue')
const Management = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Management.vue')
const Update = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Update.vue')

// import Release from '../components/Release.vue'
// import Management from '../components/Management.vue'
// import Update from '../components/Update.vue'

const Message = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Message.vue')
const Time = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Time.vue')

// import Message from '../components/Message.vue'
// import Time from '../components/Time.vue'
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: 'Login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home },
  { path: '/release', component: Release },
  { path: '/management', component: Management },
  { path: '/update', component: Update },
  { path: '/message', component: Message },
  { path: '/time', component: Time }
]
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 挂载路由导航守卫的控制
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转出来
  // next 是一个函数 表示放行
  // next() 放行  next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
